<template>
  <div class="container">
    <div class="left-nav">
      <div
        @click="currentCategory = item.id"
        v-for="item in navList"
        :key="item.id"
        :class="{
          'nav-cell': true,
          'with-grey-bottom-border': true,
          active: currentCategory == item.id
        }"
      >
        <span>{{ item.kind_name }}</span>
      </div>
    </div>
    <div class="category">
      <div class="top-search">
        <div class="input-container">
          <input
            @click="
              $router.push({
                path: '/search',
                query: {
                  action: $route.query.action
                }
              })
            "
            class="search-input"
            placeholder="请输入关键词搜索"
            type="text"
          /><button
            @click="
              $router.push({
                path: '/search',
                query: {
                  action: $route.query.action
                }
              })
            "
            class="search-btn"
          ></button>
        </div>
      </div>
      <div class="category-list">
        <div
          @click="
            $router.push({
              path: '/item-list/' + item1.id,
              query: {
                action: $route.query.action
              }
            })
          "
          class="category-cell"
          v-for="item1 in itemList"
        >
          <div class="img">
            <img :src="item1.style" alt="" />
          </div>
          <div class="category-name">{{ item1.kind_name }}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getCategory } from "@/api/mall";

export default {
  data() {
    return {
      navList: [],
      itemHash: {},
      itemList: [],
      currentCategory: null
    };
  },
  watch: {
    currentCategory: function(val) {
      this.itemList = this.itemHash[val].kind_two;
      this.$router.replace({
        path: "/category",
        query: {
          id: val,
          action: this.$route.query.action
        }
      });
    }
  },
  methods: {},
  created() {
    getCategory().then(res => {
      if (res.status === 200) {
        this.navList = res.data.data;
        this.navList.forEach(item => {
          this.itemHash[item.id] = item;
        });
        if (this.$route.query.id) {
          this.currentCategory = this.$route.query.id;
          this.$forceUpdate();
        } else {
          this.currentCategory = this.navList[0].id;
        }
      }
    });
  }
};
</script>

<style lang="less" scoped>
.container {
  height: 100%;
  display: flex;
  .top-search {
    background-color: #fff;
    padding: 0.3rem 3.2%;
    width: 100%;
    display: flex;
    justify-content: space-between;
    position: relative;
    .input-container {
      width: 100%;
      position: relative;
    }
    .search-input {
      box-sizing: border-box;
      width: 100%;
      height: 1.24rem;
      border: none;
      vertical-align: top;
      font-size: 0.56rem;
      padding: 0 0 0 4.22%;
      border-radius: 0.16rem;
      background-color: rgb(245, 245, 245);
      font-size: "微软雅黑";
    }
    .search-btn {
      position: absolute;
      width: 1.24rem;
      height: 1.24rem;
      right: 0.3rem;
      top: 0rem;
      padding: 0;
      border: 0;
      background: none;
      background-image: url("../../assets/商城首页-assets/shousuo.png");
      background-repeat: no-repeat;
      background-size: 60% 60%;
      background-position: 50% 50%;
    }
  }
  .left-nav {
    width: 30%;
    height: 100%;
    overflow: auto;
    color: rgba(68, 68, 68, 1);
    font-size: 0.56rem;

    .nav-cell {
      height: 2rem;
      transition-duration: 0.2s;
      display: flex;
      align-items: center;
      span {
        display: inline-block;
        // margin-top: 0.6rem;
        padding-left: 1rem;
        box-sizing: border-box;
        transition-duration: 0.2s;
      }
    }

    .active {
      font-weight: bold;
      color: rgba(0, 120, 255, 1);
      background-color: #fff;

      span {
        border-left: 0.24rem solid #0078ff;
      }
    }
  }

  .category {
    width: 70%;
    background-color: #fff;
    height: 100%;
    overflow: auto;

    .title {
      color: rgba(68, 68, 68, 1);
      font-weight: bold;
      font-size: 0.48rem;
      padding: 1rem 0 0.6rem 0.7rem;
    }

    .category-list {
      display: flex;
      flex-wrap: wrap;
      text-align: center;

      .category-cell {
        width: 33%;
        margin-bottom: 0.6rem;

        .img {
          height: 2.7rem;
          width: 2.7rem;
          background-color: #dfdfdf;
          margin: 0 auto 0.4rem;

          img {
            width: 100%;
            height: 100%;
            object-fit: cover;
          }
        }

        .category-name {
          color: rgba(68, 68, 68, 1);
          font-size: 0.48rem;
        }
      }
    }
  }
}
</style>
